/** @noSelfInFile **/
import {UnitModel} from "./apiModel/adapterModel/model3/UnitModel";
import {ModelFactory} from "./apiModel/adapterModel/model5/ModelFactory";
import {ItemModel} from "./apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "./apiModel/adapterModel/model4/PlayerModel";
import {PlayerFactory} from "./PlayerFactory";
import {LocModel} from "./apiModel/base/LocModel";
import {UnitJson} from "./apiModel/model/StoreJson";
import {Logger} from "./apiModel/config/Logger";
import {ItemDataKey, PlayerDataKey, UnitDataKey} from "./dataSource/DataSource";
import UnitUtil from "../utils/UnitUtil";
import {RectModel} from "./apiModel/adapterModel/model2/RectModel";
import {CodeUtil} from "./codeAdapter/CodeUtil";
import {
    PlayerEvent,
    PlayerUnitEvent,
    triggetAdapter,
    UnitEvent
} from "./apiModel/adapter/adapterInterface/model4/ITriggerAdapter";
import {TriggerModel} from "./apiModel/adapterModel/model4/TriggerModel";
import {ModelType} from "./apiModel/adapter/adapterInterface/model5/IModelFactory";

export enum SyncDataKey {
    uiEvent = 'uiEvent',
    history = 'history',
    mouseEvent = 'mouseEvent',
    keyEvent = 'keyEvent',
}

export enum MouseSyncActionType {
    useSpell = 1,
    dropTargetItem,
    moveUnit,
    clearEffectUnit,
}

export interface MouseSyncData {
    act: MouseSyncActionType;
    scX: number;
    scY: number;
    lcX: number;
    lcY: number;
    lcZ: number;
    uid: string;
}

export enum EnterType {
    forbidden  = '1',
    path = '2',
    trans = '3',
}

export enum TriggerKey {
    useItem = 'useItem',
}

export class TriggerFactory {
    static triggerController: TriggerFactory = new TriggerFactory();
    inputTrigger: TriggerModel = new TriggerModel('');


    // 通过单位或物品注册的事件使用这种方式防止内存泄漏
    static triggerMaps: { [key: string]: TriggerModel } = {};
    // 通过单位与区域名注册 回调函数
    static enterFuns: { [key: string]: { [key: string]: (rect: RectModel, activeUnit: UnitModel, player: PlayerModel) => void } } = {};

    static syncMaps: { [key: string]: { data: any, time: number } } = {};

    static triggerFuns: { [key: string]: ((...args: any[]) => void)[] } = {};

    static getInstance() {
        return TriggerFactory.triggerController;
    }

    constructor() {
        this.registerUpdate()
        // this.registerMouseWheel();
        this.registerUnitBuyItem();
        this.registerUnitSellItem();
        
        // this.registerSyncData(SyncDataKey.mouseEvent, (triggerPlayer, data) => {
        //     const mouseSyncData: MouseSyncData = data;
        //     const act = mouseSyncData.act;
        //     const locx = mouseSyncData.lcX;
        //     const locy = mouseSyncData.lcY;
        //     const locz = mouseSyncData.lcZ;
        //     const uid = mouseSyncData.uid;

        //     switch (act) {
        //         case MouseSyncActionType.dropTargetItem: {
        //             const targetItem = triggerPlayer.data[PlayerDataKey.targetItem];
        //             const selectedUnit = triggerPlayer.getLastSelectedUnit();
        //             if (selectedUnit && targetItem) {
        //                 const loc = new LocModel(locx, locy, locz);
        //                 selectedUnit.dropItem(targetItem, loc);
        //                 triggerPlayer.data[PlayerDataKey.targetItem] = undefined
        //             }
        //             break;
        //         }
        //         case MouseSyncActionType.moveUnit: {
        //             const moveUnit = triggerPlayer.getLastSelectedUnit();
        //             const targetSpellInfo = triggerPlayer.data[PlayerDataKey.targetSpellInfo];
        //             if (targetSpellInfo?.bShow) {
        //                 triggerPlayer.hideAbilityEffect();
        //             }
        //             const targetItem = triggerPlayer.data[PlayerDataKey.targetItem];
        //             if (targetItem) {
        //                 triggerPlayer.data[PlayerDataKey.targetItem] = undefined
        //             }

        //             // if (uid) {
        //             //     // Logger.toastError(uid)
        //             //     const unit = ModelFactory.getInstance().getModel(uid, ModelType.unit);
        //             //     if (unit) {
        //             //         moveUnit.issueTargetOrder(OrderStr.attack, unit);
        //             //     }
        //             //     const item = ModelFactory.getInstance().getModel(uid, ModelType.item);
        //             //     if (item) {
        //             //         moveUnit.giveItem(item);
        //             //     }
        //             // } else {
        //             //     const loc = new LocModel(locx, locy, locz);
        //             //     triggerPlayer.showMoveEffect(loc)
        //             //     moveUnit.issueOrderAt(OrderStr.move, loc);
        //             // }
        //             break;
        //         }
        //         case MouseSyncActionType.clearEffectUnit: {
        //             const targetSpellInfo = triggerPlayer.data[PlayerDataKey.targetSpellInfo];
        //             if (targetSpellInfo?.bShow) {
        //                 triggerPlayer.hideAbilityEffect();
        //             }
        //             const targetItem = triggerPlayer.data[PlayerDataKey.targetItem];
        //             if (targetItem) {
        //                 triggerPlayer.data[PlayerDataKey.targetItem] = undefined
        //             }
        //             break;
        //         }
        //         case MouseSyncActionType.useSpell: {
        //             const targetSpellInfo = triggerPlayer.data[PlayerDataKey.targetSpellInfo];
        //             const selectedUnit = triggerPlayer.getLastSelectedUnit();
        //             if (selectedUnit?.isValid()) {
        //                 const id = targetSpellInfo.id;
        //                 if (targetSpellInfo?.bShow) {
        //                     if (targetSpellInfo.type === CastType1.point) {
        //                         const loc = new LocModel(locx, locy, locz);
        //                         const ret = selectedUnit.useAbility(id, loc);
        //                         if (ret) {
        //                             triggerPlayer.hideAbilityEffect();
        //                             if (!CodeUtil.isNullOrZero(uid)) {
        //                                 triggerPlayer.clearSelecteds();
        //                             }
        //                         }
        //                     }
        //                 }
        //             }
        //             break;
        //         }
        //     }
        //     callBack(triggerPlayer, mouseEventInfo.type, mouseEventInfo.screenX, mouseEventInfo.screenY, mouseEventInfo.loc.x, mouseEventInfo.loc.y, mouseEventInfo.loc.z);
        // })
    }

    static exeSyncDatas() {
        CodeUtil.mapObj(TriggerFactory.syncMaps, (key, body) => {
            if (body) {
                const data = body.data;
                triggetAdapter.adapter.syncData(key, JSON.stringify(data));
                TriggerFactory.syncMaps[key] = undefined;
            }
        })
    }

    static damageCallBack (this: void) {
        const damageInfo = triggetAdapter.adapter.getDamageInfo();

        const damage = damageInfo.damage
        const hurtType = damageInfo.hurtType;

        const attackerModel: UnitModel = damageInfo.attacker;
        const attackedModel: UnitModel = damageInfo.attacked;

        const attackerPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(attackerModel) as PlayerModel;
        const attackedPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(attackedModel) as PlayerModel;

        if (attackerModel && attackedModel) {
        } else {
            return;
        }

        UnitUtil.customFight(attackerModel, attackedModel, attackerPlayerModel, attackedPlayerModel, damage, hurtType)

    }

    static enterCallBack (this: void) {
        const transInfo = triggetAdapter.adapter.getTransInfo();

        const activeUnitModel: UnitModel = transInfo.transUnit;
        let rect = transInfo.rect;

        if (rect) {
        } else {
            Logger.toastError('激活了一个不存在的区域')
            return;
        }

        const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(activeUnitModel) as PlayerModel;

        if (activeUnitModel) {
        } else {
            return;
        }

        const obj = TriggerFactory.enterFuns[rect.nikeName];

        CodeUtil.mapObj(obj, (key, callBack) => {
            callBack && callBack(rect, activeUnitModel, activePlayerModel)
        })

    }

    public createUnitModel<T> (playerId: number, unitJson: UnitJson, loc: LocModel, face: number) {
        const unit = ModelFactory.getInstance().createUnit(playerId, unitJson, loc, face);
        const handleId = unit.getHandleId();
        if (handleId) {
            const trigger = TriggerFactory.getInstance().registerUnitDamaged(unit);
            TriggerFactory.triggerMaps[handleId] = trigger;
        }
        return unit;
    }

    public destoryUnit (unit: UnitModel) {
        const key = unit.getHandleId();
        if (key) {
            ModelFactory.getInstance().destroy(unit, ModelType.unit)
            TriggerFactory.triggerMaps[key] = undefined;
        }
    }

    // public getFunByTriggerMap<T> (unit: UnitModel, triggerModel: TriggerModel) {
    //     const handleId = unit.getHandleId();
    //     const triggerId = triggerModel.getHandleId();
    //     if (handleId && triggerId) {
    //         if (TriggerFactory.getInstance().triggerMaps[handleId]) {
    //             return TriggerFactory.getInstance().triggerMaps[handleId][triggerId];
    //         }
    //     }
    // }

    // 建筑单位的触发
    public registerConstructFinish(callBack: (this: void, activeUnit: UnitModel, activePlayer: PlayerModel) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.constructFinish);
        const tempCallBack = function (this: void) {
            const construcInfo = triggetAdapter.adapter.getConstructInfo();
            if (construcInfo.constructUnit) {
                let unitModel: UnitModel = construcInfo.constructUnit;
                const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(unitModel) as PlayerModel;
                callBack(unitModel, activePlayerModel)
            }

        }
        model.addAction(tempCallBack);
    }

    public registerMouseWheel() {
        const model = new TriggerModel('');
        const tempCallBack = function (this: void, bStop: boolean) {
            const wheelInfo = triggetAdapter.adapter.getWheelInfo();
            let wheelNum = wheelInfo.delta;
            const activePlayerModel: PlayerModel = wheelInfo.player;

            if (bStop) {
                wheelNum = 0;
            }
            activePlayerModel.addCarmeraHeightFun(-wheelNum);
        }
        model.registerMouseWheel(tempCallBack);
    }

    public registerUpdate() {
        const model = new TriggerModel('');
        const tempCallBack = function (this: void) {
            const updateInfo = triggetAdapter.adapter.getUpdateInfo();
            const player = updateInfo.player
            if (player?.isValid()) {
                const resetCameraHeight = player.data[PlayerDataKey.resetCameraLoc];
                if (resetCameraHeight) {
                    const loc = player.cameraLoc;
                    const cameraAngle = player.cameraAngle;
                    player.cameraLoc = loc;
                    player.setCameraAngle(cameraAngle);
                    player.data[PlayerDataKey.resetCameraLoc] = false;
                }
            }
        }
        model.registerUpdate(tempCallBack);
    }

    public registerPlayerSelected(callBack: (this: void, selectedPlayer: PlayerModel) => void) {
        const model = new TriggerModel('');
        const players = PlayerFactory.getInstance().getAllNotEndPlayingPlayer();
        model.registerPlayerSelected(players, true);
        const tempCallBack = function (this: void) {
            const selectInfo = triggetAdapter.adapter.getSelectInfo();
            const playerModel = selectInfo.player;
            if (playerModel) {
                callBack(playerModel)
            }
        }
        model.addAction(tempCallBack);
    }

    public registerUnitAttacked(callBack: (this: void, attacker: UnitModel, attacked: UnitModel, attackerPlayer: PlayerModel, attackedPlayer: PlayerModel) => void) {
        const model = new TriggerModel('');

        model.registerAnyUnitEvent(PlayerUnitEvent.unitAttacked);
        const tempCallBack = function (this: void) {
            const attackInfo = triggetAdapter.adapter.getAttackInfo();
            const attackerModel: UnitModel = attackInfo.attacker;
            const attackedModel: UnitModel = attackInfo.attacked;


            const attackerPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(attackerModel) as PlayerModel;
            const attackedPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(attackedModel) as PlayerModel;

            callBack(attackerModel, attackedModel, attackerPlayerModel, attackedPlayerModel)
        }
        model.addAction(tempCallBack);
    }

    /**
     * 反复注册
     * @param unitModel
     * @param callBack
     */
    public registerUnitDamaged (unitModel: UnitModel) {
        const model = new TriggerModel('');

        model.registerUnitEvent(unitModel, UnitEvent.unitDamage);

        model.addAction(TriggerFactory.damageCallBack);
        return model;
    }

    /**
     *
     * 购买的物品也走这个方法， onlyBuy为ture时，代表仅在购买时执行
     * @param callBack
     * @param onlyBuy
     */
    public registerUnitGetItem(callBack: (this: void, getUnit: UnitModel, item: ItemModel, activePlayer: PlayerModel, seller: UnitModel) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitGetItem);
        const tempCallBack = function (this: void) {
            const manipulatorInfo = triggetAdapter.adapter.getManipulatingItemInfo();
            const unitModel: UnitModel = manipulatorInfo.unit;
            let itemModel: ItemModel = manipulatorInfo.item;

            if (unitModel) {
            } else {
                return;
            }

            const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(unitModel) as PlayerModel;

            const seller = itemModel.data[ItemDataKey.seller];

            callBack(unitModel, itemModel, activePlayerModel, seller)

            if (itemModel) {
                if (seller) {
                    itemModel.data[ItemDataKey.seller] = undefined;
                }
            }

        }
        model.addAction(tempCallBack);
    }

    public registerUnitManipulateItem(callBack: (this: void, activeUnit: UnitModel, item: ItemModel, activePlayer: PlayerModel) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitUseItem);
        model.registerAnyUnitEvent(PlayerUnitEvent.unitGetItem);
        model.registerAnyUnitEvent(PlayerUnitEvent.unitDropItem);
        const tempCallBack = function (this: void) {
            const manipulatorInfo = triggetAdapter.adapter.getManipulatingItemInfo();
            const unitModel: UnitModel = manipulatorInfo.unit;
            let itemModel: ItemModel = manipulatorInfo.item;

            const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(unitModel) as PlayerModel;

            if (unitModel) {
            } else {
                return;
            }
            //
            // if (itemModel) {
            // } else {
            //     return;
            // }

            callBack(unitModel, itemModel, activePlayerModel)
        }
        model.addAction(tempCallBack);
    }

    public registerUnitUseItem(callBack: (this: void, getUnit: UnitModel, item: ItemModel, activePlayer: PlayerModel) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitUseItem);
        const tempCallBack = function (this: void) {
            const manipulatorInfo = triggetAdapter.adapter.getManipulatingItemInfo();
            const unitModel: UnitModel = manipulatorInfo.unit;
            let itemModel: ItemModel = manipulatorInfo.item;
            // if (GetItemLifeBJ(item) > 0.4) {
            // } else {
            //     return;
            // }

            const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(unitModel) as PlayerModel;

            if (unitModel) {
            } else {
                return;
            }


            callBack(unitModel, itemModel, activePlayerModel)

            if (itemModel?.isAlive()) {
            } else {
                ModelFactory.getInstance().unitRemoveItem(unitModel, itemModel);
            }

        }
        model.addAction(tempCallBack);
    }

    public registerUnitSellItem() {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitSellItem);
        const tempCallBack = function (this: void) {
            const sellItemInfo = triggetAdapter.adapter.getSellItemInfo();
            const unitModel: UnitModel = sellItemInfo.buyUnit;
            let itemModel: ItemModel = sellItemInfo.item;

            const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(unitModel) as PlayerModel;

            // callBack(unitModel, itemModel, activePlayerModel)

            ModelFactory.getInstance().unitRemoveItem(unitModel, itemModel)
        }
        model.addAction(tempCallBack);
    }

    public registerUnitBuyItem() {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitBuyItem);
        const tempCallBack = function (this: void) {
            const sellItemInfo = triggetAdapter.adapter.getSellItemInfo();
            const unitModel: UnitModel = sellItemInfo.buyUnit;
            let itemModel: ItemModel = sellItemInfo.item;
            const sellerUnitModel: UnitModel = sellItemInfo.sellUnit;

            if (itemModel?.isValid()) {
                itemModel.data[ItemDataKey.seller] = sellerUnitModel;
            }
            if (unitModel) {
            } else {
                return;
            }

            // callBack(unitModel, itemModel, activePlayerModel)
        }
        model.addAction(tempCallBack);
    }

    /**
     * 反复注册
     * @param unitModel
     * @param callBack
     */
    public registerUnitFinishUseSpell(callBack: (this: void, activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitFinishUseSpell);
        const tempCallBack = function (this: void) {
            const spellInfo = triggetAdapter.adapter.getSpellInfo();

            const activeUnitModel: UnitModel = spellInfo.useUnit;
            const targetUnitModel: UnitModel = spellInfo.targetUnit;
            const targetItemModel: ItemModel = spellInfo.targetItem;
            const activeItemModel: ItemModel = spellInfo.useItem;

            const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(activeUnitModel) as PlayerModel;
            const targetPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(targetUnitModel) as PlayerModel;

            const spellId = spellInfo.spellId;
            const spellLoc = spellInfo.spellLoc;

            if (activeUnitModel) {
            } else {
                return;
            }

            callBack(activeUnitModel, activeItemModel, targetUnitModel, targetItemModel, activePlayerModel, targetPlayerModel, spellId, spellLoc)
        }
        model.addAction(tempCallBack);
    }

    // public registerMouseTrigger(callBack: (this: void, player: PlayerModel, mouseType: MouseEventType, screenX: number, screenY: number, locX: number, locY: number, locZ: number) => void) {
    //     const model = new TriggerModel('');
    //     const tempCallBack = function (this: void, mouseEventInfo: MouseEventInfo) {
    //         const player = mouseEventInfo.player;
    //         const loc = mouseEventInfo.loc;
    //         const percentX = mouseEventInfo.percentX;
    //         const percentY = mouseEventInfo.percentY;
    //         const type = mouseEventInfo.type;
    //         const unit = mouseEventInfo.unit;

    //         const bStop = mouseEventInfo.bStop;
    //         if (bStop) {
    //             return;
    //         }

    //         if (player) {
    //         } else {
    //             return;
    //         }

    //         let action: MouseSyncActionType;
    //         let param: any = {};
    //         let resetSize = false;

    //         const spellInfo = player.data[PlayerDataKey.targetSpellInfo];
    //         const targetItem = player.data[PlayerDataKey.targetItem];

    //         switch (type) {
    //             // case MouseEventType.mouseRightUp: {
    //             //     break;
    //             // }
    //             case MouseEventType.mouseRightDown: {
    //                 // const moveUnit = player.getLastSelectedUnit();
    //                 // const selectedUnit = player.data[PlayerDataKey.selectedUnit];
    //                 // const targetSpellInfo = player.data[PlayerDataKey.targetSpellInfo];

    //                 // Logger.toastError(`${mouseEventInfo.bStop}`);
    //                 // const targetItem = player.data[PlayerDataKey.targetItem];
    //                 // if (mouseEventInfo.bStop) {
    //                 //     // action = MouseSyncActionType.moveUnit;
    //                 // } else
    //                 // if (targetSpellInfo?.bShow || targetItem) {
    //                 //     action = MouseSyncActionType.clearEffectUnit;
    //                 // }

    //                 action = MouseSyncActionType.moveUnit;
    //                 break;
    //             }
    //             // case MouseEventType.mouseLeftUp: {
    //             //     break;
    //             // }
    //             case MouseEventType.mouseLeftDown: {
    //                 resetSize = true;
    //                 // print(JSON.stringify(spellInfo));
    //                 if (spellInfo?.bShow) {
    //                     const castType = spellInfo.type;
    //                     let success = false;
    //                     switch (castType) {
    //                         case CastType1.point: {
    //                             if (loc?.isVaild()) {
    //                                 success = true;
    //                             } else {
    //                                 Logger.toastProduct('请选择释放地点')
    //                             }
    //                             break;
    //                         }
    //                         case CastType1.unit: {
    //                             if (unit?.isValid()) {
    //                             } else {
    //                                 Logger.toastProduct('请选择释放单位')
    //                             }
    //                             break;
    //                         }
    //                     }
    //                     if (success) {
    //                         // effectModel.show = false;
    //                         action = MouseSyncActionType.useSpell;
    //                     }
    //                 } else if (targetItem) {
    //                     // print(player.data[PlayerDataKey.selectedUnit].name);
    //                     if (unit?.isValid()) {
    //                     } else {
    //                         action = MouseSyncActionType.dropTargetItem;
    //                     }
    //                 }
    //                 break;
    //             }
    //             case MouseEventType.mouseMove: {
    //                 const effectModel = spellInfo.effect;
    //                 if (spellInfo.bShow) {
    //                     effectModel.loc = loc;
    //                 }
    //                 const targetItem = player.data[PlayerDataKey.targetItem];
    //                 if (targetItem) {
    //                     targetItem.data[ItemDataKey.fakeX] = percentX;
    //                     targetItem.data[ItemDataKey.fakeY] = percentY;
    //                 }
    //                 break;
    //             }
    //         }
    //         // if (resetSize) {
    //             // player.data[PlayerDataKey.windowSize] = { width: width, height: height };
    //         // }
    //         // print(`screenX: ${screenX}, screenY: ${screenY}`);
    //         if (!CodeUtil.isNullOrZero(action)) {
    //             const data: MouseSyncData = { act: action, scX: percentX, scY: percentY, lcX: loc.x, lcY: loc.y, lcZ: loc.z, uid: unit?.getHandleId() };
    //             TriggerFactory.getInstance().SyncObjData(SyncDataKey.mouseEvent, data)
    //         }
    //     }
    //     model.registerMouseEvent(true, tempCallBack);
    // }

    // public registerMouseMoveTrigger(callBack: (this: void) => void) {
    //     const model = new TriggerModel('');
    //     model.registerMouseMoveEvent();
    //     const tempCallBack = function (this: void) {
    //         const spellInfo = triggetAdapter.adapter.getMouseMoveEventInfo();
    //
    //         const player = spellInfo.player;
    //         const loc = spellInfo.loc;
    //         const x = spellInfo.screenX;
    //         const y = spellInfo.screenY;
    //
    //         if (player) {
    //         } else {
    //             return;
    //         }
    //
    //         callBack()
    //     }
    //     model.addAction(tempCallBack);
    // }


    public registerUnitBeforeUseSpell(callBack: (this: void, activeUnit: UnitModel, targetUnit: UnitModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitBeforeUseSpell);
        const tempCallBack = function (this: void) {
            const spellInfo = triggetAdapter.adapter.getSpellInfo();

            const activeUnitModel: UnitModel = spellInfo.useUnit
            const targetUnitModel: UnitModel = spellInfo.targetUnit

            const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(activeUnitModel) as PlayerModel;
            const targetPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(targetUnitModel) as PlayerModel;


            const spellId = spellInfo.spellId;
            const spellLoc = spellInfo.spellLoc;

            if (activeUnitModel) {
            } else {
                return;
            }

            callBack(activeUnitModel, targetUnitModel, activePlayerModel, targetPlayerModel, spellId, spellLoc)
        }
        model.addAction(tempCallBack);
    }

    /**
     * 反复注册
     * @param unitModel
     * @param callBack
     */
    public registerUnitLevelUp(callBack: (this: void, activeUnit: UnitModel, activePlayer: PlayerModel, level: number, upLevel: number) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitLevelUp);
        const tempCallBack = function (this: void) {
            const levelInfo = triggetAdapter.adapter.getLevelUpInfo();
            const activeUnitModel = levelInfo.unit;
            const level = levelInfo.level;

            if (activeUnitModel) {
            } else {
                return;
            }

            const activePlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(activeUnitModel) as PlayerModel;

            const lvHistory = activeUnitModel.data[UnitDataKey.maxLvHistory];

            // 本次提升的等级
            const subLv = level - lvHistory;
            if (subLv >= 0) {
                callBack(activeUnitModel, activePlayerModel, level, subLv)
            }
        }
        model.addAction(tempCallBack);
    }

    /**
     * killer 在被使用代码杀死时为空
     * 反复注册
     * @param callBack
     */
    public registerUnitDead(callBack: (this: void, killer: UnitModel, killed: UnitModel, killerPlayer: PlayerModel, killedPlayer: PlayerModel) => void) {
        const model = new TriggerModel('');
        model.registerAnyUnitEvent(PlayerUnitEvent.unitDead);
        const tempCallBack = function (this: void) {
            const deadInfo = triggetAdapter.adapter.getDeadInfo();

            const killerUnitModel: UnitModel = deadInfo.killer;
            const killedUnitModel: UnitModel = deadInfo.killed

            const killerPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(killerUnitModel) as PlayerModel;
            const killedPlayerModel: PlayerModel = PlayerFactory.getInstance().getUnitOwner(killedUnitModel) as PlayerModel;

            if (killedUnitModel) {
            } else {
                return;
            }

            killedUnitModel.dead();

            callBack(killerUnitModel, killedUnitModel, killerPlayerModel, killedPlayerModel)
        }
        model.addAction(tempCallBack);
    }

    public registerPlayerInput(callBack: (player: PlayerModel, str: string) => void) {
        const players = PlayerFactory.getInstance().getAllNotEndPlayingPlayer();
        this.inputTrigger.registerAnyPlayerChatEvent(players, '', false);
        const tempCallBack = function (this: void) {
            const inputInfo = triggetAdapter.adapter.getInputInfo();
            const str = inputInfo.str;
            const playerModel: PlayerModel = inputInfo.player;
            callBack(playerModel, str)
        }
        this.inputTrigger.addAction(tempCallBack);
    }

    public registerKeyEvent(key: number, sync: boolean, callBack: (this: void, player: PlayerModel) => void) {
        const model = new TriggerModel('');
        model.registerKeyEvent(key, 1, sync, null);
        const tempCallBack = function (this: void) {
            const keyInfo = triggetAdapter.adapter.getKeyEventInfo();
            const playerModel: PlayerModel = keyInfo.player;
            callBack(playerModel)
        }
        model.addAction(tempCallBack);
    }

    public registerPlayerLeave(callBack: (this: void, player: PlayerModel) => void) {
        const model = new TriggerModel('');
        const players = PlayerFactory.getInstance().getAllNotEndPlayingPlayer();
        model.registerAnyPlayerEvent(players, PlayerEvent.playerLeave);
        const tempCallBack = function (this: void) {
            const playerExitInfo = triggetAdapter.adapter.getPlayerExitInfo();
            const playerModel: PlayerModel = playerExitInfo.player
            callBack(playerModel)
        }
        model.addAction(tempCallBack);
    }

    public registerSyncData(key: SyncDataKey, callBack: (this: void, triggerPlayer: PlayerModel, data: any) => void) {
        const model = new TriggerModel('');
        model.registerSyncData(key)
        const tempCallBack = function (this: void) {
            const syncDataInfo = triggetAdapter.adapter.getSyncDataInfo();
            let dataStr = syncDataInfo.str;
            let data = JSON.parse(dataStr);
            const playerModel: PlayerModel = syncDataInfo.player
            callBack(playerModel, data)
        }
        model.addAction(tempCallBack);
    }
    public SyncObjData(key: string, data: any): void {
        if (data) {
            if (TriggerFactory.syncMaps[key]) {
            } else {
                TriggerFactory.syncMaps[key] = { data: undefined, time: undefined };
                // body = TriggerFactory.syncMaps[key];
            }
            TriggerFactory.syncMaps[key].data = data;
        }
    }

    /**
     * 反复注册
     * @param unitModel
     * @param callBack
     */
    public registerUnitEnterRect(start: string, type: EnterType, callBack: (this: void, rect: RectModel, activeUnit: UnitModel, player: PlayerModel) => void) {
        const model = new TriggerModel('');
        const rectModel = ModelFactory.getInstance().getRectModel(start);
        model.registerEnterRect(rectModel);
        const name = rectModel.nikeName;
        if (name) {
            let map;
            if (TriggerFactory.enterFuns[name]) {
            } else {
                TriggerFactory.enterFuns[name] = {};
            }
            map = TriggerFactory.enterFuns[name]
            map[type] = callBack;
            model.addAction(TriggerFactory.enterCallBack);
        } else {
            Logger.toastError(`${start} 区域不存在`)
        }
    }

    public executeTrigger(triggerKey: string, ...args: any[]) {
        const funs = TriggerFactory.triggerFuns[triggerKey];
        if (funs) {
            for (let i = 0; i < funs.length; i++) {
                const fun = funs[i];
                fun(...args);
            }
        }
    }

    public registTrigger(triggerKey: string, callBack: (...args: any[]) => void) {
        let funs = TriggerFactory.triggerFuns[triggerKey];
        if (funs) {
        } else {
            TriggerFactory.triggerFuns[triggerKey] = [];
            funs = TriggerFactory.triggerFuns[triggerKey];
        }
        funs.push(callBack);
    }

}
